package com.jqh.demo.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jqh.demo.entity.AclRole;
import com.jqh.demo.service.AclRoleService;
import com.jqh.demo.service.AclUserService;
import com.jqh.demo.utils.Judge;
import com.jqh.demo.utils.R;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author jqh
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/api/acl-role")
@CrossOrigin
public class AclRoleController {
    @Autowired
    private AclRoleService aclRoleService;

    @Autowired
    private AclUserService aclUserService;


    @ApiOperation("添加角色")
    @PostMapping("addRole")
    public R addRole(@RequestBody AclRole aclRole){
        if(StringUtils.isEmpty(aclRole.getRoleName())){
            Judge.judge(false,"角色名称为空!");
        }
        boolean flag = aclRoleService.save(aclRole);
        return Judge.judge(flag,"添加失败！");
    }

    @ApiOperation("根据id删除角色")
    @DeleteMapping("{id}")
    public R delRole(@PathVariable String id){
        if(StringUtils.isEmpty(id)){
            Judge.judge(false,"参数为空!");
        }
        boolean flag = aclRoleService.removeRoleWithMid(id);
        return Judge.judge(flag,"删除失败！");
    }

    @ApiOperation("根据id列表批量删除角色")
    @DeleteMapping("batchDelRole")
    public R batchDelRole(@RequestBody List<String> idList){
        if(idList.size()==0){
            Judge.judge(false,"删除序号集为空!");
        }
        boolean flag = aclRoleService.removeByIdsWithMid(idList);
        return Judge.judge(flag,"批量删除失败！");
    }


    @ApiOperation("更新角色")
    @PutMapping("updRole")
    public R updRole(@RequestBody AclRole aclRole){
        if(StringUtils.isEmpty(aclRole.getId())){
            Judge.judge(false,"输入参数有误!");
        }
        boolean flag = aclRoleService.updateById(aclRole);
        return Judge.judge(flag,"更新失败！");
    }

    @ApiOperation("根据id获取角色")
    @GetMapping("{id}")
    public R getRoleById(@PathVariable String id){
        if(StringUtils.isEmpty(id)){
            return Judge.judge(false,"参数为空!");
        }
        AclRole role = aclRoleService.getById(id);
        return R.ok().data("role",role);
    }

    @ApiOperation("根据条件分页查询角色")
    @GetMapping("pageRole/{current}/{limit}")
    public R pageRole(@PathVariable Long current, @PathVariable Long limit, @RequestParam(value="roleNameSe",required=false) String roleName){
        Page<AclRole> page=new Page<>(current,limit);
        aclRoleService.pageQuery(page,roleName);
        return R.ok().data("row",page.getRecords()).data("total",page.getTotal());
    }
    @ApiOperation("给用户分配角色")
    @PostMapping("giveUserRole")
    public R giveUserRole(@RequestParam String userId,@RequestBody String[] roleIds){
        aclUserService.giveUserRole(userId,roleIds);
        return R.ok();
    }

    @ApiOperation("根据userId查询角色信息")
    @GetMapping("getRoleByUserId/{userId}")
    public R getRoleByUserId(@PathVariable String userId){
        if(StringUtils.isEmpty(userId)){
            Judge.judge(false,"用户参数Id为空!");
        }
        List<AclRole> roleList=aclRoleService.getRoleByUserId(userId);
        return R.ok().data("aclRoles",roleList);
    }

    @ApiOperation("获取全部角色")
    @GetMapping("getAllRole")
    public R getAllRole(){
        List<AclRole> roleList = aclRoleService.list(null);
        return R.ok().data("roleList",roleList);
    }




}

